package demo1;

class Solution4 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode newHead = new ListNode(-1);
        ListNode tmpH = newHead;
        while(list1 != null && list2 != null) {
            if(list1.val >= list2.val) {
                tmpH.next = list2;
                tmpH = list2;
                list2 = list2.next;
            }else{
                tmpH.next = list1;
                tmpH = list1;
                list1 = list1.next;
            }
        }
        if(list1 == null) {
            tmpH.next = list2;
        }
        if(list2 == null) {
            tmpH.next = list1;
        }
        return newHead.next;
    }
}